package com.web.logindemo.service.impl;

import com.web.logindemo.common.util.StringUtils;
import com.web.logindemo.domain.entity.SysRole;
import com.web.logindemo.mapper.SysRoleMapper;
import com.web.logindemo.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class SysRoleServiceImpl implements ISysRoleService {

    @Autowired
    private SysRoleMapper roleMapper;

    /**
     * 根据用户id查询权限
     *
     * @param userId
     * @return
     */
    @Override
    public Set<String> selectRolePermissionsByUserId(Long userId) {
        List<SysRole> roles = roleMapper.selectRolePermissionsByUserId(userId);
        Set<String> perms = new HashSet<>();
        for (SysRole role : roles) {
            if (StringUtils.isNotEmpty(role.getRoleKey())) {
                perms.addAll(Arrays.asList(role.getRoleKey().trim().split(",")));
            }
        }
        return perms;
    }
}
